//
//  BHProgress.m
//  Behing
//
//  Created by Lj on 2017/11/10.
//  Copyright © 2017年 lj. All rights reserved.
//

#import "BHProgress.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>

@interface BHProgress ()
@property (nonatomic, assign) NSInteger randData;
@property (nonatomic, assign) CGFloat t_start_X;
@property (nonatomic, strong) CAShapeLayer *layer;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, assign) BOOL isEnd;
@property (nonatomic, assign) BOOL isDelete;
@property (nonatomic, strong) UIBezierPath *pathWithLine;
@property (nonatomic, assign) CGFloat run_time;

@end

static BHProgress *progress = nil;
CGFloat progressPointX = 15.f; //默认位置
CGFloat progressPointY = 66.f;
CGFloat progressWidth = 100;

UIView *showView = nil;

CGFloat endTime = .3f;

@implementation BHProgress

+ (instancetype)shareInstance {
    @synchronized (self) {
        if (!progress) {
            progress = [[self alloc]init];
            srand((unsigned)time(0));
            progress.randData = progressWidth/2;
            srand((unsigned)time(0));
            progress.randData = random()%progress.randData;
            if (progress.randData == 0) progress.randData = 1;
            progress.index = 1;
            progress.dataArray = [[NSMutableArray alloc] init];
        }
        return progress;
    }
}

+ (void)showView:(UIView *)view progressCGPoint:(CGPoint)point allWidth:(CGFloat)width {
    progressPointX = point.x;
    progressPointY = point.y;
    progressWidth = width;
    showView = view;

    [self _shwoProgress];
}


+ (void)_shwoProgress {
    [self _removeAllLayer];
    [self _startLoad];
}

+ (void)_removeAllLayer {
    [self cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeStatus:) object:nil];
    for (CAShapeLayer *shap in progress.dataArray) {
        if ([shap isKindOfClass:[CAShapeLayer class]]) {
            shap.strokeEnd = 0.0;
            [shap removeFromSuperlayer];
        }
    }
    progress.randData = progressWidth/2;
    progress.index = 1;
    progress.dataArray = [[NSMutableArray alloc] init];
    progress = nil;
}

+ (void)_startLoad {
    if (progressWidth - progressPointX < 20) {
        progress.isEnd = YES;
        if ([BHProgress shareInstance].isDelete) {
            [self _hidenProGress];
        }
    }else {
        progress.isEnd = false;
        NSInteger rand_data = 0;
        srand((unsigned)time(0));
        float t_time = random()%1 + 1.5f;
        if([BHProgress shareInstance].isDelete){
            [BHProgress shareInstance].isDelete  = false;
            progress.randData = progressWidth - progressPointX;
            rand_data=  progress.randData;
            t_time = endTime;
            [self performSelector:@selector(_removeAllLayer)withObject:nil afterDelay:endTime];
        }else if(progressWidth - progressPointX < 50) {
            //        [self hidenProGress];
            srand((unsigned)time(0));
            progress.randData = random()%20;
            if (progress.randData == 0) progress.randData = 1;
            rand_data = progress.randData;
        }else {
            srand((unsigned)time(0));
            rand_data = random()%[BHProgress shareInstance].randData;
        }
        [BHProgress shareInstance].pathWithLine = [[UIBezierPath alloc] init];
        [self drawLine:rand_data time:t_time];
        
        //循环操作
        [self performSelector:@selector(changeStatus:)withObject:nil afterDelay:t_time];
    }
}

+ (void)loaded {
    [self drawLine:[BHProgress shareInstance].randData time:0.5];
}

+ (void)drawLine:(NSInteger)rand_data time:(float)time {
    [[BHProgress shareInstance].pathWithLine moveToPoint: CGPointMake(progressPointX, progressPointY)];
    [[BHProgress shareInstance].pathWithLine addLineToPoint: CGPointMake(rand_data + progressPointX, progressPointY)];
    
    progress.run_time = time;
    progressPointX = rand_data + progressPointX;
    progress.t_start_X = rand_data + progressPointX;
    progress.randData = progressWidth - progressPointX;
    
    progress.layer = [CAShapeLayer layer];
    progress.layer.frame = [UIApplication sharedApplication].keyWindow.bounds;
    progress.layer.strokeColor = Main_Color.CGColor;
    progress.layer.fillColor = Main_BackColor.CGColor;
    progress.layer.lineCap = kCALineCapSquare;
    progress.layer.path = progress.pathWithLine.CGPath;
    progress.layer.lineWidth = 3;
    progress.layer.strokeStart = 0.0f;
    progress.layer.strokeEnd = 1.0;
    //    proGress.pathWithLine.lineJoinStyle = kCGLineJoinRound;
    progress.pathWithLine.lineJoinStyle = kCGLineCapRound;

    [showView.layer addSublayer:progress.layer];
    [progress.dataArray addObject:progress.layer];
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = time;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.f];
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.autoreverses = NO;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [progress.layer addAnimation:pathAnimation forKey:nil];
}


+ (void)_hidenProGress {
    if (progress) {
        [BHProgress shareInstance].isDelete = YES;
        if (progress.isEnd) {
            [BHProgress shareInstance].pathWithLine = [[UIBezierPath alloc]init];
            [self drawLine:progressWidth - progressPointX + 30 time:endTime];
            [self performSelector:@selector(_removeAllLayer)withObject:nil afterDelay:endTime];
        }
    }
}


+ (void)changeStatus:(NSDictionary *)dataDic {
    [self _startLoad];
}



@end
